# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.jsonUtils_v2.table2Json_YM import Table2JsonYM
from com.zzh.utils.pagingUtils import MyPaging
from com.zzh.utils.requestFieldMapDBField import requestFieldMapToDBField_YM
from com.zzh.utils.sqlUtils.SqlUtils import SqlUtils
import time
import sys
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
    reload(sys)
    sys.setdefaultencoding(default_encoding)

localTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
class viewUser_pointLog_dao(object):
    def __init__(self):  # 类初始化。
        self.conn = MysqlUtils.get_mysql_connections_v2()

    def queryViewUserPointLogByCondition(self, **kwargs):

        conditions = kwargs['conditions']
        multiSort = kwargs['multiSort']
        pageIndex = kwargs['pageIndex']
        pageSize = kwargs['pageSize']

        fieldMap = requestFieldMapToDBField_YM.viewUserAndPointLogMap#要改的这个
        whereSentence = SqlUtils.getOption(conditions, fieldMap)

        cur = self.conn.cursor()
        if multiSort:
            # 排序拼接
            sortRule = SqlUtils.getSortRule(multiSort, fieldMap)
            sql = "SELECT * FROM viewUser_pointLog WHERE " + whereSentence + " ORDER BY " + sortRule
        else:
            sql = "SELECT * FROM viewUser_pointLog WHERE " + whereSentence


        total_count = cur.execute(sql)
        if total_count:
            result = cur.fetchall()

            cur.close()
            self.conn.close()
            resultList= MyPaging().getPage(total_count, pageIndex, pageSize, result)
            userOauthJSON = Table2JsonYM.viewUser_pointLog_list_to_json(resultList)

            return userOauthJSON, total_count
        else:

            cur.close()
            self.conn.close()
            return None, 0